home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / pippki.jar / content / pippki / password.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  8.4 KB  |  312 lines

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is mozilla.org code.
  13.  *
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 2001 Netscape Communications Corporation. All
  17.  * Rights Reserved.
  18.  *
  19.  * Contributor(s):
  20.  *  Bob Lord <lord@netscape.com>
  21.  *  Terry Hayes <thayes@netscape.com>
  22.  */
  23. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  24. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  25. const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
  26. const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
  27. const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
  28. const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
  29. const nsIPK11Token = Components.interfaces.nsIPK11Token;
  30.  
  31.  
  32. var params;
  33. var tokenName="";
  34. var pw1;
  35.  
  36. function onLoad()
  37. {
  38.  
  39.   pw1 = document.getElementById("pw1");
  40.   try {
  41.      params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
  42.      tokenName = params.GetString(1);
  43.   }catch(exception)
  44.      {tokenName = self.name;}
  45.  
  46.   if(tokenName=="" || tokenName=="_blank") {
  47.      var sectokdb = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  48.      var tokenList = sectokdb.listTokens();
  49.      var enumElement;
  50.      var i=0;
  51.      var menu = document.getElementById("tokenMenu");
  52.      try {
  53.         for ( ; !tokenList.isDone(); tokenList.next()) {
  54.            enumElement = tokenList.currentItem();
  55.            var token = enumElement.QueryInterface(nsIPK11Token);
  56.            if(token.needsLogin() || !(token.needsUserInit)) {
  57.               var menuItemNode = document.createElement("menuitem");
  58.               menuItemNode.setAttribute("value", token.tokenName);
  59.               menuItemNode.setAttribute("label", token.tokenName);
  60.               menu.firstChild.appendChild(menuItemNode);
  61.               if (i == 0) {
  62.                  menu.selectedItem = menuItemNode;
  63.                  tokenName = token.tokenName;
  64.               }
  65.               i++;
  66.            }
  67.         }
  68.      }catch(exception){}
  69.   } else {
  70.     var sel = document.getElementById("tokenMenu");
  71.     sel.setAttribute("hidden", "true");
  72.     var tag = document.getElementById("tokenName");
  73.     tag.setAttribute("value",tokenName);
  74.   }
  75.           
  76.   process();
  77. }
  78.  
  79. function onMenuChange()
  80. {
  81.    //get the selected token
  82.    var list = document.getElementById("tokenMenu");
  83.    tokenName = list.value;
  84.  
  85.    process();
  86. }
  87.  
  88.  
  89. function process()
  90. {
  91.    var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  92.    var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  93.  
  94.    // If the token is unitialized, don't use the old password box.
  95.    // Otherwise, do.
  96.  
  97.    var slot = secmoddb.findSlotByName(tokenName);
  98.    if (slot) {
  99.      var oldpwbox = document.getElementById("oldpw");
  100.      var msgBox = document.getElementById("message");
  101.      var status = slot.status;
  102.      if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
  103.          || status == nsIPKCS11Slot.SLOT_READY) {
  104.       
  105.        oldpwbox.setAttribute("hidden", "true");
  106.        msgBox.setAttribute("value", bundle.GetStringFromName("password_not_set")); 
  107.        msgBox.setAttribute("hidden", "false");
  108.  
  109.        if (status == nsIPKCS11Slot.SLOT_READY) {
  110.          oldpwbox.setAttribute("inited", "empty");
  111.        } else {
  112.          oldpwbox.setAttribute("inited", "true");
  113.        }
  114.       
  115.        // Select first password field
  116.        document.getElementById('pw1').focus();
  117.     
  118.      } else {
  119.        // Select old password field
  120.        oldpwbox.setAttribute("hidden", "false");
  121.        msgBox.setAttribute("hidden", "true");
  122.        oldpwbox.setAttribute("inited", "false");
  123.        oldpwbox.focus();
  124.      }
  125.    }
  126.  
  127.   if (params) {
  128.     // Return value 0 means "canceled"
  129.     params.SetInt(1, 0);
  130.   }
  131.   
  132.   checkPasswords();
  133. }
  134.  
  135. function onP12Load()
  136. {
  137.   pw1 = document.getElementById("pw1");
  138.   params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
  139.   // Select first password field
  140.   document.getElementById('pw1').focus();
  141. }
  142.  
  143. function setPassword()
  144. {
  145.   var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  146.   var token = pk11db.findTokenByName(tokenName);
  147.  
  148.   var oldpwbox = document.getElementById("oldpw");
  149.   var initpw = oldpwbox.getAttribute("inited");
  150.   var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  151.   
  152.   var success = false;
  153.   
  154.   if (initpw == "false" || initpw == "empty") {
  155.     try {
  156.       var oldpw = "";
  157.       var passok = 0;
  158.       
  159.       if (initpw == "empty") {
  160.         passok = 1;
  161.       } else {
  162.         oldpw = oldpwbox.value;
  163.         passok = token.checkPassword(oldpw);
  164.       }
  165.       
  166.       if (passok) {
  167.         if (initpw == "empty" && pw1.value == "") {
  168.           // This makes no sense that we arrive here, 
  169.           // we reached a case that should have been prevented by checkPasswords.
  170.         } else {
  171.           token.changePassword(oldpw, pw1.value);
  172.           if (pw1.value == "") {
  173.             alert(bundle.GetStringFromName("pw_erased_ok")
  174.                   + " "
  175.                   + bundle.GetStringFromName("pw_empty_warning"));
  176.           } else {
  177.             alert(bundle.GetStringFromName("pw_change_ok")); 
  178.           }
  179.         }
  180.         success = true;
  181.       } else {
  182.         oldpwbox.focus();
  183.         oldpwbox.setAttribute("value", "");
  184.         alert(bundle.GetStringFromName("incorrect_pw")); 
  185.       }
  186.     } catch (e) {
  187.       alert(bundle.GetStringFromName("failed_pw_change")); 
  188.     }
  189.   } else {
  190.     token.initPassword(pw1.value);
  191.     if (pw1.value == "") {
  192.       alert(bundle.GetStringFromName("pw_not_wanted")
  193.             + " " 
  194.             + bundle.GetStringFromName("pw_empty_warning"));
  195.     }
  196.     success = true;
  197.   }
  198.  
  199.   // Terminate dialog
  200.   if (success) {
  201.     if (params) {
  202.       // Return value 1 means "successfully executed ok"
  203.       params.SetInt(1, 1);
  204.     }
  205.  
  206.     window.close();
  207.   }
  208. }
  209.  
  210. function getPassword()
  211. {
  212.   // grab what was entered
  213.   params.SetString(2, pw1.value);
  214.   // Return value
  215.   params.SetInt(1, 1);
  216.   // Terminate dialog
  217.   window.close();
  218. }
  219.  
  220. function setP12Password()
  221. {
  222.   // grab what was entered
  223.   params.SetString(2, pw1.value);
  224.   // Return value
  225.   params.SetInt(1, 1);
  226.   // Terminate dialog
  227.   window.close();
  228. }
  229.  
  230. function setPasswordStrength()
  231. {
  232. // Here is how we weigh the quality of the password
  233. // number of characters
  234. // numbers
  235. // non-alpha-numeric chars
  236. // upper and lower case characters
  237.  
  238.   var pw=document.getElementById('pw1').value;
  239. //  alert("password='" + pw +"'");
  240.  
  241. //length of the password
  242.   var pwlength=(pw.length);
  243.   if (pwlength>5)
  244.     pwlength=5;
  245.  
  246.  
  247. //use of numbers in the password
  248.   var numnumeric = pw.replace (/[0-9]/g, "");
  249.   var numeric=(pw.length - numnumeric.length);
  250.   if (numeric>3)
  251.     numeric=3;
  252.  
  253. //use of symbols in the password
  254.   var symbols = pw.replace (/\W/g, "");
  255.   var numsymbols=(pw.length - symbols.length);
  256.   if (numsymbols>3)
  257.     numsymbols=3;
  258.  
  259. //use of uppercase in the password
  260.   var numupper = pw.replace (/[A-Z]/g, "");
  261.   var upper=(pw.length - numupper.length);
  262.   if (upper>3)
  263.     upper=3;
  264.  
  265.  
  266.   var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
  267.  
  268.   // make sure we're give a value between 0 and 100
  269.   if ( pwstrength < 0 ) {
  270.     pwstrength = 0;
  271.   }
  272.   
  273.   if ( pwstrength > 100 ) {
  274.     pwstrength = 100;
  275.   }
  276.  
  277.   var mymeter=document.getElementById('pwmeter');
  278.   mymeter.setAttribute("value",pwstrength);
  279.  
  280.   return;
  281. }
  282.  
  283. function checkPasswords()
  284. {
  285.   var pw1=document.getElementById('pw1').value;
  286.   var pw2=document.getElementById('pw2').value;
  287.  
  288.   var ok=document.getElementById('ok-button');
  289.  
  290.   var oldpwbox = document.getElementById("oldpw");
  291.   if (oldpwbox) {
  292.     var initpw = oldpwbox.getAttribute("inited");
  293.  
  294.     if (initpw == "empty" && pw1 == "") {
  295.       // The token has already been initialized, therefore this dialog
  296.       // was called with the intention to change the password.
  297.       // The token currently uses an empty password.
  298.       // We will not allow changing the password from empty to empty.
  299.       ok.setAttribute("disabled","true");
  300.       return;
  301.     }
  302.   }
  303.  
  304.   if (pw1 == pw2){
  305.     ok.setAttribute("disabled","false");
  306.   } else
  307.   {
  308.     ok.setAttribute("disabled","true");
  309.   }
  310.  
  311. }
  312.